package com.lgx.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.dao.IDictionaryDao;
import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.service.IDictionaryService;
import com.lgx.common.util.LayerOrderUtils;
import com.lgx.common.web.util.DictUtils;

/**
 * 
 * @author acer
 *
 */
@Service("dictionaryService")
public class DictionaryService implements IDictionaryService {
	
	@Autowired
	private IDictionaryDao dictionaryDao;
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	public IDictionaryDao getDictionaryDao() {
		return dictionaryDao;
	}

	public void setDictionaryDao(IDictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}

	/* (non-Javadoc)
	 * @see com.lgx.common.service.IDictionaryService#createDictionary(com.lgx.common.entity.Dictionary)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Dictionary createDictionary(Dictionary d) {
		
		if(d.getParent() != null)
		{
			String layerOrder = LayerOrderUtils.createLayerOrder("TS_DICTIONARY", d.getParent().getId()+"", this.baseDao);
			d.setLayerOrder(layerOrder);
			Dictionary parent = d.getParent();
			parent.setLeaf(false);
			try {
				this.dictionaryDao.updateDictionary(parent);
			} catch (DataException e) {
				e.printStackTrace();
			}
		}else
		{
			d.setLayerOrder(LayerOrderUtils.DEFAULT_LAYERORDER);
		}
		try {
			d.setLeaf(true);
			this.dictionaryDao.createDictionary(d);
			
			//更新缓存
			List<Object> objs = this.initDictionary();
	    	DictUtils.kinds = (List<Dictionary>) objs.get(0);
	    	DictUtils.dicts = (List<List<Dictionary>>) objs.get(1);
			
			return d;
		} catch (DataException e) {
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Dictionary updateDictionary(Dictionary d) {
		try {
			this.dictionaryDao.updateDictionary(d);
			//更新缓存
			List<Object> objs = this.initDictionary();
	    	DictUtils.kinds = (List<Dictionary>) objs.get(0);
	    	DictUtils.dicts = (List<List<Dictionary>>) objs.get(1);
			
			return d;
		} catch (DataException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void deleteDictionary(Dictionary d) {
		try {
			this.dictionaryDao.deleteDictionary(d);
		} catch (DataException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteDictionary(long id) {
		try {
			this.dictionaryDao.deleteDictionary(id+"");
		} catch (DataException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Dictionary findDictionaryById(long id) {
		return this.dictionaryDao.findDictionaryById(id+"");
	}

	@Override
	public List<Dictionary> findDictionaryList() {
		return this.dictionaryDao.findDictionaryList();
	}

	@Override
	public Map<String, List<Dictionary>> initDictionaryMap() {
		Map<String, List<Dictionary>> map = new HashMap<String, List<Dictionary>>();
		List<Dictionary> kinds = this.dictionaryDao.findDictionaryKind();
		for(Dictionary kind : kinds){
			List<Dictionary> dicts = this.dictionaryDao.findDictionaryList(kind.getKind());
			map.put(kind.getKind(), dicts);
		}
		
		return map;
	}

	@Override
	public List<Dictionary> findDictionaryList(String kind) {
		return this.dictionaryDao.findDictionaryList(kind);
	}

	@Override
	public List<Object> initDictionary() {
		List<Object> result = new ArrayList<Object>();
		List<Dictionary> kinds = this.dictionaryDao.findDictionaryKind();
		result.add(kinds);
		List<List<Dictionary>> values = new ArrayList<List<Dictionary>>();
		for(Dictionary kind : kinds){
			List<Dictionary> dicts = this.dictionaryDao.findDictionaryList(kind.getKind());
			values.add(dicts);
		}
		result.add(values);
		return result;
	}

}
